package task

import (
	"sync"
)
type HiMonitorQueue struct {
	queue HiQueue
	cond sync.Cond
	exit bool
}

func NewHiMonitorQueue() HiMonitorQueue {
	var queue = new(HiMonitorQueue)
	queue.queue = NewHiQueue()
	mutex  := new(sync.Mutex)
	queue.cond = *sync.NewCond(mutex)
	queue.exit = false
	return *queue
}

func (q*HiMonitorQueue)lock() {
	q.cond.L.Lock()
}
func (q*HiMonitorQueue)unLock() {
	q.cond.L.Unlock()
}

func (q*HiMonitorQueue)Size() int {
	q.lock()
	defer q.unLock()
	var size =  q.queue.Size()
	return size
}

func  (q*HiMonitorQueue)Put(value interface{}) {
	if q.exit {
		return
	}
	q.lock()
	defer q.unLock()
	q.queue.Add(value)
	q.cond.Signal()
}

func  (q*HiMonitorQueue)Take() interface{} {
	if q.exit {
		return nil
	}
	q.lock()
	defer q.unLock()
	if (q.queue.Size() < 1) {
		q.cond.Wait()
	}
	if (q.exit) {
		return nil
	}
	var val = q.queue.Take()
	return val
}

func (q*HiMonitorQueue)Quit() {
	if q.exit {
		return
	}
	q.lock()
	defer q.unLock()
	q.exit = true
	q.cond.Broadcast()
}

func (q*HiMonitorQueue)IsQuit() bool {
	return q.exit
}




